package academic.ui.client.presenter;


import java.util.ArrayList;

import academic.ui.client.AppServiceAsync;
import academic.ui.client.event.LogOutEvent;
import academic.ui.client.event.LogOutEventHandler;
import academic.ui.client.event.LoggedInEvent;
import academic.ui.client.event.LoggedInEventHandler;
import academic.ui.client.event.NewGameEvent;
import academic.ui.client.event.NewGameEventHandler;
import academic.ui.client.jquery.JQuery;
import academic.ui.client.place.Tokens;
import academic.ui.client.widgets.Flip;
import academic.ui.client.jquery.Facebook;
import academic.ui.client.widgets.tools.ToolTip;
import ecommerce.soco.shared.GWT_Product;


import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;

public class GamePresenter extends Presenter<GamePresenter.MyView, GamePresenter.MyProxy>
{

	private final AppServiceAsync service;
	
	private final PlaceManager placeManager;
			
	@ContentSlot
	public static final Type<RevealContentHandler<?>> slot = new Type<RevealContentHandler<?>>();
	
	
	@Inject 
	public GamePresenter(EventBus eventBus, MyView view, MyProxy proxy, AppServiceAsync service,PlaceManager placeManager) {
		super(eventBus, view, proxy);
		this.placeManager=placeManager;
		this.service = service;
	}

	@ProxyStandard
	public interface MyProxy extends Proxy<GamePresenter>  {}

	
	public interface MyView extends View {

		void setup();

		HasClickHandlers signInLink();

		HasClickHandlers newGame();

		void startGame(ArrayList<GWT_Product> result);

		void reset();

		ArrayList<HasClickHandlers> getDealHandlers();

		void updateLoggedOut();

		HasClickHandlers myAccount();

		void resetLoggedIn();

		ArrayList<HasClickHandlers> buyNowHandlers();

		void noActiveDeals();
	}
	
	@Override
	public void onReveal() {
		super.onReveal();
		Log.debug("GamePresenter onReveal");
		
			getView().setup();
			Flip.quickFlip("quickFlip");
			Flip.quickFlip("quickFlip2");
			Flip.quickFlip("quickFlip3");
	}
	
	@Override
	public void onBind() {
		super.onBind();
		Log.debug("GamePresenter onBind");
		registerHandler(getView().signInLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				placeManager.revealPlace(new PlaceRequest(Tokens.login).with("selector", "signInFlipLink"));
			}		
		}));
		registerHandler(getView().newGame().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				placeManager.revealPlace(new PlaceRequest(Tokens.deals));
			}
			
		}));
	
		
		registerHandler(getEventBus().addHandler(LogOutEvent.getType(), new LogOutEventHandler() {

			@Override
			public void onLogOutEvent(LogOutEvent event) {
				getView().updateLoggedOut();
			}
			
		}));
		
		registerHandler(getView().myAccount().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				placeManager.revealPlace(new PlaceRequest(Tokens.myAccount).with("tab", AccountPresenter.MYACCOUNTTAB));
			}
			
		}));
		
		registerHandler(getEventBus().addHandler(NewGameEvent.getType(), new NewGameEventHandler() {

			@Override
			public void onNewGameEvent(NewGameEvent event) {

				service.getActiveProducts(new AsyncCallback<ArrayList<GWT_Product>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(ArrayList<GWT_Product> result) {
						if(result==null || result.size()<1){
							getView().noActiveDeals();
						}
						else
						{
						getView().startGame(result);
						JQuery.slideTimerUp();
						for(HasClickHandlers dealHandle : getView().getDealHandlers())
						{
							dealHandle.addClickHandler(new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									placeManager.revealPlace(new PlaceRequest(Tokens.productFull).with("id", event.getRelativeElement().getId()));
								}
							});
						}
						
						for(HasClickHandlers buyNowHandle : getView().buyNowHandlers())
						{
							buyNowHandle.addClickHandler(new ClickHandler() {

								@Override
								public void onClick(ClickEvent event) {
									Log.debug("buy Now Link Clicked");
									placeManager.revealPlace(new PlaceRequest(Tokens.checkout).with("id", event.getRelativeElement().getId()));
								}
								
							});
						}
						
						DeferredCommand.addCommand(new Command() {

							@Override
							public void execute() {
								ToolTip.initToolTip();
							}
							
						});
						
						}
					}	
				});
			}
			
		}));
		
		
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, PrimaryPresenter.slot, this);
		Log.debug("GamePresenter revealInParent");
	}
	
	@Override
	public void onReset() {
		super.onReset();
		
		
		if(placeManager.getCurrentPlaceRequest().getNameToken().equals(Tokens.primary))
		{
			service.isLoggedIn(new AsyncCallback<Boolean>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(Boolean result) {
					if(result) {
						getView().resetLoggedIn();
						placeManager.revealPlace(new PlaceRequest(Tokens.deals));
					}
					getView().reset();
				}
				
			});
			
		}
			
		DeferredCommand.addCommand(new Command() {

			@Override
			public void execute() {
				Log.debug("GamePresenter onReset DeferredCommand");
				ToolTip.initToolTip();
			}
			
		});
		
		Log.debug("GamePresenter onReset");
	}
	
	@Override
	public void onHide() {
		super.onHide();
		Log.debug("GamePresenter onHide");
	}
	
	

}
